<?php

require_once $_SERVER['DOCUMENT_ROOT']."/../_lib/base/common.php";
require_once SITE_ROOT_PATH."/_lib/classes/model/SupplierPropertiesLow.php";

class SupplierPropertiesLowController {
    
    private $file;
    
    function __construct ($file){
        $this->setFile($file);
    }    
    
    public function getFile(){
        return $this->file;
    }
    
    public function setFile($file){
        $this->file = $file;
    }
       
    public function loadPropertiesFromFile(){
        
       $h = fopen($this->file, "r");
       
       if (!$h) {
            return false;
       } 
       
       $this->renameTable();
       $this->createTable();
       $this->deleteTable();

       while (($data=fgetcsv($h, 2000,chr(27))) !== FALSE) {

           $row = in($data[0]);
           $row = explode('|', $row);
           if(intval($row[0])!==0){           
               $supplierPropertiesLow = new SupplierPropertiesLow($row);      
               $supplierPropertiesLow->save();
           }
       }  
       return $this->dropTable();  
    }
    
    public function downloadPhotos(){
        error_log("Started: ".date("Y-m-d H:i:s") , 0);
        ini_set('max_execution_time', 0);
        $supplierPropertiesLow = new SupplierPropertiesLow(array()); 
        $allPhotos = $supplierPropertiesLow->getAllPhotos();
        $localPhotos = array('LocalImage1URL' => '',
                             'LocalImage2URL' => '',
                             'LocalImage3URL'=> '',
                             'PropertyID' => ''); 
        foreach ($allPhotos as $photo) {
            $localPhotos['LocalImage1URL'] = $supplierPropertiesLow->savePhoto($photo['Image1URL']);
            $localPhotos['LocalImage2URL'] = $supplierPropertiesLow->savePhoto($photo['Image2URL']);
            $localPhotos['LocalImage3URL'] = $supplierPropertiesLow->savePhoto($photo['Image3URL']);            
            $localPhotos['PropertyID'] = $photo['PropertyID'];
            error_log("[".date("Y-m-d H:i:s") . "] PropertyID: ". $localPhotos['PropertyID'].
                       ", Image1URL: " .$photo['Image1URL'].                    
                       ", Image2URL: " .$photo['Image2URL'].                    
                       ", Image3URL: " .$photo['Image3URL'].                    
                       ", LocalImage1URL: " .$localPhotos['LocalImage1URL'].
                       ", LocalImage2URL: " .$localPhotos['LocalImage2URL'].
                       ", LocalImage3URL: " .$localPhotos['LocalImage3URL'], 0);
            $supplierPropertiesLow->updateLocalPhotos($localPhotos);            
            //$supplierPropertiesLow->insertPhotosIntoTemporaryTable($localPhotos);            
        }
        error_log("Finished: ".date("Y-m-d H:i:s") , 0);

    }
    
    private function createTable(){     
        $sql = "CREATE TABLE supplier_properties_low AS SELECT * FROM SUPPLIER_PROPERTIES_LOW_BKP ";
        return mysql_query($sql) or die (mysql_error());
    }  
 
    private function renameTable(){
         $sql =  "ALTER TABLE supplier_properties_low RENAME TO SUPPLIER_PROPERTIES_LOW_BKP";     
         return mysql_query($sql) or die (mysql_error());
    }
 
    private function dropTable(){
          $sql = "DROP TABLE SUPPLIER_PROPERTIES_LOW_BKP";
          return mysql_query($sql) or die (mysql_error());
    }
 
    private function deleteTable(){
          $sql = "DELETE FROM supplier_properties_low";
          return mysql_query($sql) or die (mysql_error());
    }
    
}
?>